$(function () {
	vm.uploadBtn = new AjaxUpload('#upload', {
        action: '../filemanager/upload',
        name: 'file',
        autoSubmit:false,
        responseType:"json",
        onSubmit:function(file, extension){
            if (!(extension && /^(jpg|jpeg|png|gif|doc|docx|xls|xlsx|ppt|pptx|txt)$/.test(extension.toLowerCase()))){
                alert('只支持jpg、png、gif格式的图片和office文档！');
                return false;
            }
        },
        onChange: function (file, ext) {
        	$('#fileName').val(file);
        },
        onComplete : function(file, r){
            if(r.code == 0){
                alert('操作成功');
                vm.reload();
            }else{
                alert(r.msg);
            }
        }
    });
	
    $("#jqGrid").jqGrid({
        url: '../filemanager/list',
        datatype: "json",
        colModel: [			
            //{ label: 'id', name: 'id', index: 'id', width: 80, key: true },
			{ label: '名称', name: 'name', index: 'name', width: 80},
			{ label: '文件名', name: 'fileName', index: 'file_name', width: 80 , formatter: function(cellvalue, options, rowObject) {   
		        	return vm.showOrDownload(cellvalue, rowObject);
		    	} 
			}, 
			{ label: '扩展名', name: 'fileExtName', index: 'file_ext_name', width: 80 }, 			
			//{ label: '文件路径', name: 'filePath', index: 'file_path', width: 80 }, 			
			{ label: '生成URL', name: 'shortUrl', index: 'short_url', width: 80 }, 			
			{ label: '文件大小', name: 'size', index: 'size', width: 80 } 			
			//{ label: '备注', name: 'remark', index: 'remark', width: 80 }			
        ],
		viewrecords: true,
        height: 385,
        rowNum: 10,
		rowList : [10,30,50],
        rownumbers: true, 
        rownumWidth: 25, 
        autowidth:true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader : {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames : {
            page:"page", 
            rows:"limit", 
            order: "order"
        },
        gridComplete:function(){
        	//隐藏grid底部滚动条
        	$("#jqGrid").closest(".ui-jqgrid-bdiv").css({ "overflow-x" : "hidden" }); 
        }
    });
});

var vm = new Vue({
	el:'#rrapp',
	data:{
		showList: true,
		q:{
			key: null
		},
		uploadBtn : null,
		title: null,
		imgUrl : null,
		fileManager: {}
	},
	methods: {
		query: function () {
			vm.reload();
		},
		add: function(){
			vm.showList = false;
			vm.title = "新增";
			vm.fileManager = {};
			
		},
		showImg : function(url) {
			vm.imgUrl = '../f/d/' + url;
			layer.open({
				type:1,
				//skin: 'demo-class',
				title: "查看图片",
				area: ['550px', '470px'],
				shadeClose: false,
				shade: 0.000001,
				anim : 4,
				resize : true,
				content: $("#passwordLayer"),
				btn: ['确认'],
				btn1: function (index) {
					layer.close(index);
	            }
			});
		},
		update: function (event) {
			var id = getSelectedRow();
			if(id == null){
				return ;
			}
			vm.showList = false;
            vm.title = "修改";
            vm.getInfo(id)
		},
		saveOrUpdate: function (event) {
			vm.uploadBtn.setData(vm.fileManager);
			vm.uploadBtn.submit();
			return;
			/*var url = vm.fileManager.id == null ? "../filemanager/save" : "../filemanager/update";
			$.ajax({
				type: "POST",
			    url: url,
			    contentType: "application/json",
			    data: JSON.stringify(vm.fileManager),
			    success: function(r){
			    	if(r.code === 0){
						alert('操作成功', function(index){
							vm.reload();
						});
					}else{
						alert(r.msg);
					}
				}
			});*/
		},
		del: function (event) {
			var ids = getSelectedRows();
			if(ids == null){
				return ;
			}
			
			confirm('确定要删除选中的记录？', function(){
				$.ajax({
					type: "POST",
				    url: "../filemanager/delete",
				    contentType: "application/json",
				    data: JSON.stringify(ids),
				    success: function(r){
						if(r.code == 0){
							alert('操作成功', function(index){
								$("#jqGrid").trigger("reloadGrid");
							});
						}else{
							alert(r.msg);
						}
					}
				});
			});
		},
		getInfo: function(id){
			$.get("../filemanager/info/"+id, function(r){
                vm.fileManager = r.fileManager;
            });
		},
		reload: function (event) {
			vm.showList = true;
			var page = $("#jqGrid").jqGrid('getGridParam','page');
			$("#jqGrid").jqGrid('setGridParam',{ 
				postData:{'key': vm.q.key},
                page:page
            }).trigger("reloadGrid");
		},
		showOrDownload : function(cellValue, rowObject) {
			var extName = rowObject.fileExtName;
			if(/^(jpg|jpeg|png|gif)$/.test(extName.toLowerCase())) {
				return '<a href="javascript:vm.showImg(\'' + rowObject.shortUrl + '\');">' + cellValue + '</a>';
			} else {
				return '<a href="../f/x/' + rowObject.shortUrl + '">' + cellValue + '</a>';
			}
		}
	}
});



















